package itsm.isperp.module.service.bpm.act;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.DataRequest;
import itsm.isperp.framework.data.domain.DataResponse;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.bpm.act.ActRuTask;
import itsm.isperp.module.repository.bpm.act.ActRuTaskMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * BPM对引擎操作的服务
 * 
 * 
 * @author lizx
 * @date 2013-7-1
 * 
 */
@Service
public class ActRuTaskService {

	@Autowired
	ActRuTaskMapper actRuTaskMapper;

	public Long getTaskCount(String username) {
		return this.actRuTaskMapper.findTaskCountByUsernameAndRequest(username);
	}

	public DataResponse<ActRuTask> getTaskList(String username, DataRequest req) {

		return this.actRuTaskMapper.findTaskListByUsernameAndRequest(username,
				req.setSelectColumns(" DISTINCT task.* "));
	}

	public Long getMyTaskCount() {
		return this.getTaskCount(ContextHolder.getLoginUsername());
	}

	/**
	 * 查询当前登录用户的任务列表
	 * 
	 * @param req
	 *            数据请求对象
	 * @return 结果
	 */
	public DataResponse<ActRuTask> getMyTaskList(DataRequest req) {

		return getTaskList(ContextHolder.getLoginUsername(), req);
	}

	public DataResponse<ActRuTask> findTaskListByRequest(DataRequest req) {
		req.setSelectColumns(" DISTINCT * ");

		JqGridRequest r = (JqGridRequest) req;
		if (r.getSidx() != null) {

			for (int i = 0; i < r.getSidx().length; i++) {
				r.getSidx()[i] = r.getSidx()[i] + "_";
			}
		}

		DataResponse<ActRuTask> k = this.actRuTaskMapper
				.findTaskListByRequest(req);
		return k;
	}

}
